package nz.co.doherty.gwtpbreadcrumbs.client.ui.products;

import nz.co.doherty.gwtpbreadcrumbs.client.NameTokens;
import nz.co.doherty.gwtpbreadcrumbs.client.gin.ClientGinjector;
import nz.co.doherty.gwtpbreadcrumbs.client.i8n.ClientConstants;
import nz.co.doherty.gwtpbreadcrumbs.client.ui.app.AppPresenter;
import nz.co.doherty.gwtpbreadcrumbs.shared.entity.Product;
import nz.co.doherty.gwtpbreadcrumbs.shared.rpc.product.ProductCreateAction;
import nz.co.doherty.gwtpbreadcrumbs.shared.rpc.product.ProductCreateResult;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.philbeaudoin.gwtp.dispatch.client.DispatchAsync;
import com.philbeaudoin.gwtp.mvp.client.EventBus;
import com.philbeaudoin.gwtp.mvp.client.PresenterImpl;
import com.philbeaudoin.gwtp.mvp.client.annotations.NameToken;
import com.philbeaudoin.gwtp.mvp.client.annotations.ProxyCodeSplit;
import com.philbeaudoin.gwtp.mvp.client.annotations.TitleFunction;
import com.philbeaudoin.gwtp.mvp.client.proxy.PlaceManager;
import com.philbeaudoin.gwtp.mvp.client.proxy.ProxyPlace;
import com.philbeaudoin.gwtp.mvp.client.proxy.RevealContentEvent;

public class ProductCreatePresenter
		extends
		PresenterImpl<ProductCreateView, ProductCreatePresenter.ProductCreateProxy>
		implements ProductCreateView.Presenter {

	@ProxyCodeSplit
	@NameToken(NameTokens.productCreate)
	public interface ProductCreateProxy extends
			ProxyPlace<ProductCreatePresenter> {
	}

	private final DispatchAsync dispatcher;
	private final PlaceManager placeManager;
	private final ClientConstants constants;

	@Inject
	public ProductCreatePresenter(EventBus eventBus, ProductCreateView view,
			ProductCreateProxy proxy, PlaceManager placeManager,
			ClientConstants constants, DispatchAsync dispatcher) {
		super(eventBus, view, proxy);
		this.placeManager = placeManager;
		this.constants = constants;
		this.dispatcher = dispatcher;
	}

	@Override
	protected void onBind() {
		super.onBind();
	}

	@Override
	protected void onReset() {
		super.onReset();
		this.view.reset();
	}

	@Override
	protected void onReveal() {
		super.onReveal();
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(eventBus, AppPresenter.TYPE_RevealMainContent,
			this);
	}

	@TitleFunction
	public static String getPageTitle(ClientGinjector ginjector) {
		return ginjector.getConstants().createProduct();
	}

	@Override
	public void onCreate() {
		Product product = new Product(this.view.getName());
		this.dispatcher.execute(new ProductCreateAction(product), new AsyncCallback<ProductCreateResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(ProductCreateResult result) {
				// TODO Auto-generated method stub
				placeManager.revealRelativePlace(-1);
			}		
		});		
	}
}
